<!DOCTYPE html>
<html>
<!--
Copyright 2006 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by an Apache 2.0 License.
See the COPYING file for details.
-->
<head>
<title>Closure Unit Tests - goog.dom.classes</title>=
<script src="../base.js"></script>
<script>
  goog.require('goog.dom');
  goog.require('goog.dom.classes');
  goog.require('goog.testing.jsunit');
</script>
<style type="text/css">
#styleTest1 {
  width:120px;font-weight:bold;
}
</style>
</head>
<body>
  <div id="testEl">
    <span>Test Element</span>
  </div>

  <div><div><div id="testEl2"></div></div></div>

  <span id="span1" class="test1"></span>
  <span id="span2" class="test1"></span>
  <span id="span3" class="test2"></span>
  <span id="span4" class="test3"></span>
  <span id="span5" class="test1"></span>
  <span id="span6"></span>

  <p id="p1"></p>

  <div id="styleTest1"></div>
  <div id="styleTest2" style="width:100px;font-weight:bold"></div>
  <div id="styleTest3"></div>

  <!-- Paragraph to test element child and sibling -->
  <p id="p2">
    <!-- Comment -->
    a
    <b id="b1">c</b>
    d
    <!-- Comment -->
    e
    <b id="b2">f</b>
    g
    <!-- Comment -->
  </p>
  <p id="p3" class="someclass
                    otherclass">
    h
  </p>
  <iframe name="frame" src="../math/math_test.html"></iframe>

<script>

  var $ = goog.dom.getElement;
  var classes = goog.dom.classes;

  function testSetAddHasRemove() {
    var el = $('p1');
    classes.set(el, 'someclass');
    assertTrue('Should have someclass', classes.has(el, 'someclass'));

    classes.set(el, 'otherclass');
    assertTrue('Should have otherclass', classes.has(el, 'otherclass'));
    assertFalse('Should not have someclass', classes.has(el, 'someclass'));

    classes.add(el, 'wooclass');
    assertTrue('Should have otherclass', classes.has(el, 'otherclass'));
    assertTrue('Should have wooclass', classes.has(el, 'wooclass'));

    classes.add(el, 'aclass', 'bclass', 'cclass');
    assertTrue('Should have otherclass', classes.has(el, 'otherclass'));
    assertTrue('Should have wooclass', classes.has(el, 'wooclass'));
    assertTrue('Should have aclass', classes.has(el, 'aclass'));
    assertTrue('Should have bclass', classes.has(el, 'bclass'));
    assertTrue('Should have cclass', classes.has(el, 'cclass'));

    classes.remove(el, 'cclass');
    assertTrue('Should have otherclass', classes.has(el, 'otherclass'));
    assertTrue('Should have wooclass', classes.has(el, 'wooclass'));
    assertTrue('Should have aclass', classes.has(el, 'aclass'));
    assertTrue('Should have bclass', classes.has(el, 'bclass'));
    assertFalse('Should not have cclass', classes.has(el, 'cclass'));

    classes.remove(el, 'aclass', 'bclass');
    assertTrue('Should have otherclass', classes.has(el, 'otherclass'));
    assertTrue('Should have wooclass', classes.has(el, 'wooclass'));
    assertFalse('Should not have aclass', classes.has(el, 'aclass'));
    assertFalse('Should not have bclass', classes.has(el, 'bclass'));
  }

  function testSwap() {
    var el = $('p1');
    classes.set(el, 'someclass first');

    assertTrue('Should have first class', classes.has(el, 'first'));
    assertTrue('Should have first class', classes.has(el, 'someclass'));
    assertFalse('Should not have second class', classes.has(el, 'second'));

    classes.swap(el, 'first', 'second');

    assertFalse('Should not have first class', classes.has(el, 'first'));
    assertTrue('Should have first class', classes.has(el, 'someclass'));
    assertTrue('Should have second class', classes.has(el, 'second'));

    classes.swap(el, 'second', 'first');

    assertTrue('Should have first class', classes.has(el, 'first'));
    assertTrue('Should have first class', classes.has(el, 'someclass'));
    assertFalse('Should not have second class', classes.has(el, 'second'));
  }

  function testEnable() {
    var el = $('p1');
    classes.set(el, 'someclass first');

    assertTrue('Should have first class', classes.has(el, 'first'));
    assertTrue('Should have someclass class', classes.has(el, 'someclass'));

    classes.enable(el, 'first', false);

    assertFalse('Should not have first class', classes.has(el, 'first'));
    assertTrue('Should have someclass class', classes.has(el, 'someclass'));

    classes.enable(el, 'first', true);

    assertTrue('Should have first class', classes.has(el, 'first'));
    assertTrue('Should have someclass class', classes.has(el, 'someclass'));
  }

  function testToggle() {
    var el = $('p1');
    classes.set(el, 'someclass first');

    assertTrue('Should have first class', classes.has(el, 'first'));
    assertTrue('Should have someclass class', classes.has(el, 'someclass'));

    classes.toggle(el, 'first');

    assertFalse('Should not have first class', classes.has(el, 'first'));
    assertTrue('Should have someclass class', classes.has(el, 'someclass'));

    classes.toggle(el, 'first');

    assertTrue('Should have first class', classes.has(el, 'first'));
    assertTrue('Should have someclass class', classes.has(el, 'someclass'));
  }

  function testAddNotAddingMultiples() {
    var el = $('span6');
    classes.add(el, 'a');
    assertEquals('a', el.className);
    classes.add(el, 'a');
    assertEquals('a', el.className);
    classes.add(el, 'b', 'b');
    assertEquals('a b', el.className);
  }

  function testAddRemoveString() {
    var el = $('span6');
    el.className = 'a';

    goog.dom.classes.addRemove(el, 'a', 'b');
    assertEquals('b', el.className);

    goog.dom.classes.addRemove(el, null, 'c');
    assertEquals('b c', el.className);

    goog.dom.classes.addRemove(el, 'c', 'd');
    assertEquals('b d', el.className);

    goog.dom.classes.addRemove(el, 'd', null);
    assertEquals('b', el.className);
  }

  function testAddRemoveArray() {
    var el = $('span6');
    el.className = 'a';

    goog.dom.classes.addRemove(el, ['a'], ['b']);
    assertEquals('b', el.className);

    goog.dom.classes.addRemove(el, [], ['c']);
    assertEquals('b c', el.className);

    goog.dom.classes.addRemove(el, ['c'], ['d']);
    assertEquals('b d', el.className);

    goog.dom.classes.addRemove(el, ['d'], []);
    assertEquals('b', el.className);
  }

  function testAddRemoveMultiple() {
    var el = $('span6');
    el.className = 'a';

    goog.dom.classes.addRemove(el, ['a'], ['b', 'c', 'd']);
    assertEquals('b c d', el.className);

    goog.dom.classes.addRemove(el, [], ['e', 'f']);
    assertEquals('b c d e f', el.className);

    goog.dom.classes.addRemove(el, ['c', 'e'], []);
    assertEquals('b d f', el.className);

    goog.dom.classes.addRemove(el, ['b'], ['g']);
    assertEquals('d f g', el.className);
  }

  function testHasWithNewlines() {
    var el = $('p3');
    assertTrue('Should have someclass', classes.has(el, 'someclass'));
    assertTrue('Should also have otherclass', classes.has(el, 'otherclass'));
    assertFalse('Should not have weirdclass', classes.has(el, 'weirdclass'));
  }

</script>
</body>
</html>
